Skip to contentMethod: PersistenceUnitTestRunner(Logger, PersistenceFactory, DataAccessor)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.runner;
19:
20: import cz.cvut.kbss.jopa.model.EntityManager;
21: import cz.cvut.kbss.jopa.test.OWLClassA;
22: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
23: import cz.cvut.kbss.jopa.test.environment.Generators;
24: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
25: import org.junit.jupiter.api.Test;
26: import org.slf4j.Logger;
27:
28: import java.net.URI;
29: import java.util.ArrayList;
30: import java.util.List;
31:
32: import static org.junit.jupiter.api.Assertions.assertEquals;
33: import static org.junit.jupiter.api.Assertions.assertNotNull;
34:
35: public abstract class PersistenceUnitTestRunner extends BaseRunner {
36:
37: public PersistenceUnitTestRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
38: super(logger, persistenceFactory, dataAccessor);
39: }
40:
41: @Test
42: void multiplePersistenceUnitsSaveDataIndependently() {
43: final List<EntityManager> ems = initPersistenceUnits();
44: try {
45: for (EntityManager em : ems) {
46: em.getTransaction().begin();
47: em.persist(entityA);
48: em.getTransaction().commit();
49: em.clear();
50: }
51:
52: for (EntityManager em : ems) {
53: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
54: // Cannot use count query, because OWL2Query does not support it
55: final List<?> res = em.createNativeQuery("SELECT ?x WHERE {?x a ?type .}")
56: .setParameter("type", URI.create(OWLClassA.getClassIri())).getResultList();
57: assertEquals(1, res.size());
58: }
59: } finally {
60: ems.forEach(em -> {
61: em.close();
62: em.getEntityManagerFactory().close();
63: });
64: }
65: }
66:
67: private List<EntityManager> initPersistenceUnits() {
68: final List<EntityManager> ems = new ArrayList<>();
69: for (int i = 0; i < Generators.randomPositiveInt(2, 5); i++) {
70: ems.add(getEntityManager("MultiplePUsTest" + i, false));
71: }
72: return ems;
73: }
74: }